package org.shangrila.app.sys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.shangrila.app.sys.eo.SysAuthority;
import org.shangrila.app.sys.eo.SysRole;
import org.shangrila.app.sys.eo.SysUser;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.utils.MsgsUtil;

@Named
@SessionScoped
public class SysUserAuthMB implements Serializable {

	private static final long serialVersionUID = 1L;

	private String userno;
	
    private SysUser selectedUser;    //当前用户
	private List<SysUser> voSysUsers;

	private List<SysAuthority> voSetted=new ArrayList<SysAuthority>();			//已用的权限
	private List<SysAuthority> voSelections=new ArrayList<SysAuthority>();		//没有的-可以供选择的权限
	private List<SysAuthority> selectedAuths=new ArrayList<SysAuthority>();     //提交时需要设置的权限
	private List<SysAuthority> voSelected=new ArrayList<SysAuthority>();		//选择待设置的权限
	private List<SysAuthority> voAuthorities=new ArrayList<SysAuthority>(); 	//所有权限集合

	private List<SysRole> voRSetted=new ArrayList<SysRole>();		//已用的角色
	private List<SysRole> voRSelections=new ArrayList<SysRole>();	//没有的-可以供选择的角色
	private List<SysRole> selectedRoles=new ArrayList<SysRole>();    //提交时需要设置的角色
	private List<SysRole> voRSelected=new ArrayList<SysRole>();		//选择待设置的角色
	private List<SysRole> voRoles=new ArrayList<SysRole>(); 		//所有角色集合

	private DbAccess db = new DbAccess();
	
	public SysUserAuthMB(){
		refreshAction();
		
	}
	public void refreshAction(){	
		voSysUsers = db.findEntityAll("SysUser");
		selectedUser = voSysUsers.get(0);
		userno = selectedUser.getUserno();
		
		voSetted = selectedUser.getSysAuthorities();
		voRSetted = selectedUser.getSysRoles();
		
		voAuthorities = db.findEntityAll("SysAuthority");
		voRoles = db.findEntityAll("SysRole");
		
		//移除已经拥有的权限
		Iterator<SysAuthority> iter = voSelections.iterator();  
		while(iter.hasNext()){
			if(voSetted.contains(iter.next()))
		        iter.remove();  
		} 
		//移除已经拥有的角色
		Iterator<SysRole> iter1 = voRSelections.iterator();  
		while(iter1.hasNext()){
			if(voRSetted.contains(iter1.next()))
		        iter1.remove();  
		} 
		MsgsUtil.showMessages("数据刷新", "成功");

	}
	
    //基于权限的授权提交
	public String authAction(){		
		voSetted.addAll(selectedAuths);//把新的权限添加到已有权限列表
		selectedUser.setSysAuthorities(voSetted);
		db.beginTransaction();
		db.update(selectedUser);
		db.commitTransaction();	
		voSelections.removeAll(selectedAuths);

		MsgsUtil.showMessages("授权", "授予选定权限成功。");
		return null;
	}
	//基于权限的授权回收
	public String revokeAction(){
		voSetted.removeAll(voSelected);//把新的权限从已有权限列表中移除		
		voSelections.addAll(voSelected);
		selectedUser.setSysAuthorities(voSetted);		
		db.beginTransaction();
		db.update(selectedUser);
		db.commitTransaction();			

		MsgsUtil.showMessages("授权", "回收选定权限成功。");
		return null;
	}	
	//基于角色的授权提交
	@SuppressWarnings("unchecked")
	public String authRoleAction(){
		//设置角色信息
		voRSelections.removeAll(voRSelected);
		voRSetted.addAll(selectedRoles);//把新的权限添加到已有权限列表
		selectedUser.setSysRoles(voRSetted);
		
		//根据角色设置权限
		voSelected.removeAll(voSelected);
		voSelected.addAll(selectedUser.getSysAuthorities());	
		for(SysRole ro:selectedRoles){
			voSelected.addAll(ro.getSysAuthorities());	
		}		
		//利用HashSet去重
		selectedAuths = removeDuplicateObject(voSelected);
		selectedUser.setSysAuthorities(selectedAuths);
		
		db.beginTransaction();
		db.update(selectedUser);
		db.commitTransaction();	
		
		voRSelections.removeAll(selectedRoles);
		voSelected.removeAll(voSelected);
		selectedAuths.removeAll(selectedAuths);

		MsgsUtil.showMessages("授权", "授予选定角色成功。");
		return null;
	}
	//基于角色的授权回收
	public String revokeRoleAction(){	
		//回收角色信息
		voRSelections.addAll(voRSelected);
		voRSetted.removeAll(voRSelected);//把新的权限添加到已有权限列表		
		selectedUser.setSysRoles(voRSetted);
		//根据角色回收权限信息
		selectedAuths.removeAll(selectedAuths);
		selectedAuths.addAll(selectedUser.getSysAuthorities());	
		for(SysRole ro:selectedRoles){
			selectedAuths.removeAll(ro.getSysAuthorities());	
		}		
		selectedUser.setSysAuthorities(selectedAuths);
		
		db.beginTransaction();
		db.update(selectedUser);
		db.commitTransaction();		

		MsgsUtil.showMessages("授权", "回收选定角色成功。");
		return null;
	}	

    public void onAuthRowSelect(SelectEvent event) {
    	selectedAuths.clear();
    	selectedAuths.addAll(voSelected);
    	selectedRoles.clear();
    	selectedRoles.addAll(voRSelected);   	
     }
 
    public void onAuthRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysAuthority) event.getObject()).getName());
    }
    public void onAuthoredRowSelect(SelectEvent event) {
    	//MsgsUtil.showMessages("Authority Selected", ((SysAuthority) event.getObject()).getName());
     }
 
    public void onAuthoredRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysAuthority) event.getObject()).getName());
    }

    public void onUserRowSelect(SelectEvent event) {
    	//MsgsUtil.showMessages("Authority Selected", ((SysUser) event.getObject()).getUsername());
    	selectedUser = (SysUser) event.getObject();
    	userno = selectedUser.getUserno();
    	
		voSetted = selectedUser.getSysAuthorities();
    	voSelections.removeAll(voAuthorities);
    	voSelections.addAll(voAuthorities);
    	
		voRSetted = selectedUser.getSysRoles();		     	
    	voRSelections.removeAll(voRoles);
    	voRSelections.addAll(voRoles);
	
		//移除已经拥有的权限
		Iterator<SysAuthority> iter = voSelections.iterator();  
		while(iter.hasNext()){
			if(voSetted.contains(iter.next()))
		        iter.remove();  
		}
		Iterator<SysRole> iter1 = voRSelections.iterator();  
		while(iter1.hasNext()){
			if(voRSetted.contains(iter1.next()))
		        iter1.remove();  
		} 
     }
 
    public void onUserRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysUser) event.getObject()).getUsername());
    }
	
	//利用HashSet去重
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List removeDuplicateObject(List list) {
		HashSet set = new HashSet();
        List retList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(((SysAuthority)element).getId()))
            	retList.add(element);
        }
        return retList;
    }
	
	public String getUserno() {
		return userno;
	}

	public void setUserno(String userno) {
		this.userno = userno;
	}

	public SysUser getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(SysUser selectedUser) {
		this.selectedUser = selectedUser;
	}

	public List<SysUser> getVoSysUsers() {
		return voSysUsers;
	}

	public void setVoSysUsers(List<SysUser> voSysUsers) {
		this.voSysUsers = voSysUsers;
	}

	public List<SysAuthority> getVoSetted() {
		return voSetted;
	}

	public void setVoSetted(List<SysAuthority> voSetted) {
		this.voSetted = voSetted;
	}

	public List<SysAuthority> getVoSelections() {
		return voSelections;
	}

	public void setVoSelections(List<SysAuthority> voSelections) {
		this.voSelections = voSelections;
	}

	public List<SysAuthority> getSelectedAuths() {
		return selectedAuths;
	}

	public void setSelectedAuths(List<SysAuthority> selectedAuths) {
		this.selectedAuths = selectedAuths;
	}

	public List<SysAuthority> getVoSelected() {
		return voSelected;
	}

	public void setVoSelected(List<SysAuthority> voSelected) {
		this.voSelected = voSelected;
	}

	public List<SysAuthority> getVoAuthorities() {
		return voAuthorities;
	}

	public void setVoAuthorities(List<SysAuthority> voAuthorities) {
		this.voAuthorities = voAuthorities;
	}

	public List<SysRole> getVoRSetted() {
		return voRSetted;
	}

	public void setVoRSetted(List<SysRole> voRSetted) {
		this.voRSetted = voRSetted;
	}

	public List<SysRole> getVoRSelections() {
		return voRSelections;
	}

	public void setVoRSelections(List<SysRole> voRSelections) {
		this.voRSelections = voRSelections;
	}

	public List<SysRole> getSelectedRoles() {
		return selectedRoles;
	}

	public void setSelectedRoles(List<SysRole> selectedRoles) {
		this.selectedRoles = selectedRoles;
	}

	public List<SysRole> getVoRSelected() {
		return voRSelected;
	}

	public void setVoRSelected(List<SysRole> voRSelected) {
		this.voRSelected = voRSelected;
	}

	public List<SysRole> getVoRoles() {
		return voRoles;
	}

	public void setVoRoles(List<SysRole> voRoles) {
		this.voRoles = voRoles;
	}

}